package dsx.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

public class DateFormat {

    static ConcurrentHashMap<String, SimpleDateFormat> formatMap = new ConcurrentHashMap<String, SimpleDateFormat>();

    private static SimpleDateFormat getFormat(String format) {
        if (formatMap.containsKey(format)) {
            return formatMap.get(format);
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            formatMap.put(format, dateFormat);
            return dateFormat;
        }
    }

    public static String toString(Date date, String format) {
        SimpleDateFormat dateFormat = getFormat(format);
        return dateFormat.format(date);
    }

    public static String now(String format) {
        SimpleDateFormat dateFormat = getFormat(format);
        return dateFormat.format(new Date());
    }

}
